/*
 * 防抖
 * s fn [function] 需要防抖的函数
 * delay [number] 毫秒，防抖期限值
 */
// const debounce = (fn, delay) => {
// 	let timer = null
// 	return function() {
// 		if (timer) {
// 			clearTimeout(timer)
// 		}
// 		timer = setTimeout(fn, delay)
// 	}
// }

/*
 * 节流
 * fn [function] 需要节流的函数
 * delay [number] 毫秒，节流期限值
 */
// const throttle = (fn, delay) => {
// 	let valid = true
// 	return function() {
// 		if (!valid) {
// 			//休息时间 暂不接客
// 			return false
// 		}
// 		// 工作时间，执行函数并且在间隔期内把状态位设为无效
// 		valid = false
// 		setTimeout(() => {
// 			fn()
// 			valid = true;
// 		}, delay)
// 	}
// }

// 防抖
export function debounce(func, wait, immediate = true) {
	let timer = null;
	const _debounce = function(...args) {
	  const context = this;
	  if (timer) {
		clearTimeout(timer)
	  }
	  if (immediate) {
		if (!timer) { // null
		  func.apply(this, args);
		}
		timer = setTimeout(() => {
		  timer = null;
		}, wait)
	  } else {
		timer = setTimeout(() => {
		  func.apply(context, args);
		  timer = null;
		}, wait)
	  }
	}
	_debounce.cancel = function() {
	  clearTimeout(timer);
	  timer = null;
	}
	return _debounce;
  }
  
  // 节流
  export function throttle(func, wait, options = {}) {
	let timer = null;
  
	const _throttle = function(...args) {
	  const context = this;
	  if (!timer) {
		timer = setTimeout(() => {
		  func.apply(context, args);
		  timer = null;
		}, wait)
	  }
	}
	return _throttle;
  }
  